package com.miyu.system.service.book.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.pojo.book.BookPage;
import com.miyu.common.service.book.BookService;
import com.miyu.common.utils.commonUtils.Comutils;
import com.miyu.common.utils.magic.GetBeanUtil;
import com.miyu.common.utils.redis.redisUtils.RedisCache;

import javax.annotation.Resource;
import java.util.concurrent.Callable;

/**
 * 收集书本作业
 * @author miyu
 * @date 2022/12/01
 */
public class CollectBookJob implements Callable<Boolean> {

    private BookPage bookPage;
    static RedisCache rdc = GetBeanUtil.getBeanByClass(RedisCache.class);

     BookService bookService = GetBeanUtil.getBeanByClass(BookService.class);
    @Override
    public Boolean call() throws Exception {
        String key = "book:collect:book";
        System.out.println("collect:"+Thread.currentThread().getName());

        JSONObject bookDetail = bookService.queryBookDetail(bookPage);
        if(bookDetail==null){
            //没有明细url 跳出
            return null;
        }
        bookPage.setUrl(bookDetail.getString("chapterUrl"));
        bookPage.setKey("book:collect:temp:"+ Comutils.MD5(bookDetail.getString("chapterUrl")));
        JSONArray chapter = bookService.queryBookChapter(bookPage);
        JSONObject book = new JSONObject();
        book.put("main",bookDetail);
        book.put("chapter",chapter);
        rdc.append(key,book);

        return null;
    }

   public CollectBookJob (BookPage bookPage){
        this.bookPage = bookPage;
   }
}
